package light.mvc.service.item.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.item.TxRay;
import light.mvc.page.base.PageFilter;
import light.mvc.page.item.XRay;
import light.mvc.service.item.XRayServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class XRayServiceImpl implements XRayServiceI {

	@Autowired
	private BaseDaoI<TxRay> xRayDao;

	@Override
	public void add(XRay xRay) {
		TxRay t = new TxRay();
		BeanUtils.copyProperties(xRay, t);
		xRayDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		TxRay t = xRayDao.get(TxRay.class, id);
		xRayDao.delete(t);
	}

	@Override
	public void edit(XRay xRay) {
		TxRay t = xRayDao.get(TxRay.class,xRay.getId());
		BeanUtils.copyProperties(xRay, t);
		xRayDao.update(t);
	}

	@Override
	public XRay get(Integer id) {
		TxRay t = xRayDao.get(TxRay.class,id);
		XRay xRay = new XRay();
		BeanUtils.copyProperties(t, xRay);
		
		return xRay;
	}
	
	@Override
	public List<XRay> dataGrid(XRay xRaySome, PageFilter ph) {
		List<XRay> xRays = new ArrayList<XRay>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from TxRay t ";
		List<TxRay> list = xRayDao.find(hql + whereHql(xRaySome, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (TxRay t : list) {
			XRay xRay = new XRay();
			BeanUtils.copyProperties(t, xRay);
			xRays.add(xRay);
		}
		return xRays;
	}
	
	@Override
	public Long count(XRay xRay, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from TxRay t ";
		return xRayDao.count("select count(*) " + hql + whereHql(xRay, params), params);
	}
	
	private String whereHql(XRay xRay, Map<String, Object> params) {
		String hql = "";
		if (xRay != null) {
			hql += " where 1=1 ";
			if (xRay.getPartOfBody()!= null) {
				hql += " and t.partOfBody like :partOfBody";
				params.put("name", "%" + xRay.getPartOfBody() + "%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public XRay getByPartOfBody(XRay xRaySome) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("partOfBody", xRaySome.getPartOfBody());
		TxRay t = xRayDao.get("from TxRay t where t.partOfBody = :partOfBody",params);
		XRay xRay = new XRay();
		if(t!=null){
			BeanUtils.copyProperties(t, xRay);
		}else{
			return null;
		}
		return xRay;
	}
	
	@Override
	public List<XRay> list() {
		List<XRay> xRays = new ArrayList<XRay>();
		String hql = " from TxRay t ";
		List<TxRay> list = xRayDao.find(hql);
		for (TxRay t : list) {
			XRay xRay = new XRay();
			BeanUtils.copyProperties(t, xRay);
			xRays.add(xRay);
		}
		return xRays;
	}
}
